:- use_module(library(lists)).
:- use_module(library('clp/bounds')).

%increases the Num element of Old list and return a New list

inc_arr(Num,Step,Old,New) :- 
		var(Step),
		var(New),
		length(Old,Step),
		length(New,Step),
		inc_arr(Num,Step,Old,New).

inc_arr(_,0,_,New) :- not(var(New)),!.


inc_arr(Num,Step,Old,New) :- 
		not(var(New)),
		Num #= Step, 
		nth1(Step,Old,Tmp),
		Tmp1 #= Tmp + 1,
		nth1(Step,New,Tmp1),
		Step1 #= Step - 1,
		inc_arr(Num,Step1,Old,New),
		nth1(Step,New,Tmp2),
		not(var(Tmp2)).
		
inc_arr(Num,Step,Old,New) :- 
		not(var(New)),
		Num #\= Step, 
		nth1(Step,Old,Tmp),
		nth1(Step,New,Tmp),
		Step1 #= Step - 1,
		inc_arr(Num,Step1,Old,New),
		nth1(Step,New,Tmp2),
		not(var(Tmp2)).


%creates a list of size Size and initialize it by zeros
create_arr(Arr,Size) :-
	length(Arr,Size),
	init_arr(Arr,0).

init_arr([],_) :-!.
	
init_arr(Arr,Char) :-
	Arr = [Char|Tail],
	init_arr(Tail,Char).

%sets the Num element of Old array at value Val and returns New array
set_arr(Num,Old,Val,New,Step):-
	var(Step),
	var(New),
	length(Old,Step),
	length(New,Step),
	set_arr(Num,Old,Val,New,Step).

set_arr(_,_,_,New,0) :- not(var(New)),!.


set_arr(Num,Old,Val,New,Step) :- 
	not(var(New)),
	Num #= Step, 
	nth1(Step,New,Val),
	Step1 #= Step - 1,
	set_arr(Num,Old,Val,New,Step1),
	nth1(Step,New,Tmp2),
	not(var(Tmp2)).
		
set_arr(Num,Old,Val,New,Step) :- 
	not(var(New)),
	Num #\= Step, 
	nth1(Step,Old,Tmp),
	nth1(Step,New,Tmp),
	Step1 #= Step - 1,
	set_arr(Num,Old,Val,New,Step1),
	nth1(Step,New,Tmp2),
	not(var(Tmp2)).



